Use case: Detect temperature thresholds (non-blocking)

Note

In HAL2, threshold configuration is simplified: use the dedicated API HAL_DTS_SetSensorThresholds(), which accepts High and Low thresholds directly in degrees Celsius. This replaces the manual calculation required in HAL1.

You can check your reference manual to get the formula used in HAL1 to convert register values to degrees Celsius.

Sequence diagram

  @startuml
  participant "User Application" as User
  participant "HAL DTS Driver" as DTS #19DD3A
  participant "NVIC" as NVIC
  participant "HW" as HW
  == Process: non-blocking mode ==

  User -> DTS: HAL_DTS_Start_IT()

  User <-- DTS: HAL_OK

  loop
  alt Temperature is Under High threshold value / Over Low threshold value
      HW <- : External HW trigger
      note left
      <font color=blue>only with hardware trigger configuration
      end note
      note over HW
      <font color=red>Measuring ended without Threshold detection
      end note

  else Temperature is Over High threshold value / Under Low threshold value
      HW <- : External HW trigger
      note left
      <font color=blue>only with hardware trigger configuration
      end note

      NVIC <- HW : High or Low threshold detection interrupt
      DTS <- NVIC : HAL_DTS_IRQHandler
      alt When using PCLK as clock source
          User <- DTS: HAL_DTS_LowCallback \n(or HAL_DTS_HighCallback)
      else When using LSE as clock source
          User <- DTS: HAL_DTS_AsyncLowCallback \n(or HAL_DTS_AsyncHighCallback)
      end
      User --> DTS:
      DTS --> NVIC :
  end

  User -> DTS: HAL_DTS_GetTemperature()
  User <-- DTS: HAL_OK
  end

  User -> DTS: HAL_DTS_Stop_IT()
  User <-- DTS: HAL_OK
  @enduml

Code snippet

HAL1

HAL2

DTS_HandleTypeDef hdts;
int32_t temp;

HAL_DTS_Init(&hdts);
hdts.Init.HighThreshold = 100; /* register readable value that needs to be */
hdts.Init.LowThreshold = 10;   /* calculated to select the desired threshold in degrees Celsius */
/* For example: Temperature: 40
   register writable value: 60
   given: ramp coeff (r): 6080, Calibration frequency (r): 1875000Hz, T0: 3D, using LSE: 32000Hz
   These values depend on your specific device and configuration, check your reference manual for more details */
HAL_DTS_Start_IT(&hdts);
HAL_DTS_GetTemperature(&hdts, &temp);
HAL_DTS_Stop_IT(&hdts);
hal_dts_handle_t hDTS;
int32_t temp;

HAL_DTS_Init(&hDTS, HAL_DTS);
hal_dts_config_t cfg = { .reference_clock = HAL_DTS_REFERENCE_CLOCK_PCLK,
                         .quick_measurement = HAL_DTS_QUICK_MEASUREMENT_DISABLE,
                         .high_speed_clock_division = 0 };
hal_dts_sensor_config_t sensor_cfg = { .trigger = HAL_DTS_TRIGGER_SOFTWARE,
                                       .sampling_time = HAL_DTS_SAMPLING_TIME_1_CYCLE };
HAL_DTS_SetConfig(&hDTS, &cfg);
HAL_DTS_SetConfigSensor(&hDTS, HAL_DTS_SENSOR_1, &sensor_cfg);
/* Configure thresholds (degC) using dedicated API */
HAL_DTS_SetSensorThresholds(&hDTS, HAL_DTS_SENSOR_1, 100, 10);
/* Enable low threshold optional interrupt */
HAL_DTS_StartSensor_IT_Opt(&hDTS, HAL_DTS_SENSOR_1, HAL_DTS_OPT_IT_LOW_THRESHOLD);
HAL_DTS_GetSensorTemperature(&hDTS, HAL_DTS_SENSOR_1, &temp);
HAL_DTS_StopSensor_IT(&hDTS, HAL_DTS_SENSOR_1);

Key changes

  1. HAL2 configures thresholds in degrees Celsius using HAL_DTS_SetSensorThresholds().

  2. HAL2 enables optional threshold interrupts using HAL_DTS_StartSensor_IT_Opt().